﻿using System;
using System.Collections.Generic;
using System.Text;
using Castle.Services.Transaction;
using MiragePWF.NH.Logging;
using MiragePWF.NH.Repositories;
using NHibernate.Criterion;
using Wolfut.MirageEE.Domain.Common;
using Wolfut.MirageEE.Domain.CommonUtilities;
using Wolfut.MirageEE.Interface.Common;

namespace Wolfut.MirageEE.Server.Server.Common
{
    [Transactional]
    [AutoLogging]
    public class SubjectsService : ISubjectsService
    {
        private readonly IRepository<Subject> subjectRepository;
        public SubjectsService(IRepository<Subject> subjectRepository)
        {
            this.subjectRepository = subjectRepository;
        }
        #region ISubjectsService 成员

        /// <summary>
        /// 增加科目
        /// </summary>
        /// <param name="subject"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.RequiresNew)]
        public Subject AddSubject(Subject subject)
        {
            if (subject == null)
            {
                throw new Exception("NullReference!");
            }

            subjectRepository.Save(subject);
            //if (subject.IsDefault)
            //{
            //    IList<Subject> lists = this.ListCodeRulesByCodeFlag(coderule.CodeFlag, false);
            //    lists.Remove(coderule);
            //    foreach (CodeRule item in lists)
            //    {
            //        item.IsDefault = false;
            //        codeRuleRepository.Update(item);
            //    }
            //}
            return subject;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subject"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.Requires)]
        public Subject UpdateSubject(Subject subject)
        {
            subjectRepository.Update(subject);
            return subject;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subjectID"></param>
        /// <returns></returns>
        public Subject GetSubjectByID(string subjectID)
        {
            return subjectRepository.Find(subjectID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="listSubjectID"></param>
        [Transaction(TransactionMode.Requires)]
        public void DeleteList(IList<string> listSubjectID)
        {
            foreach(string subjectID in listSubjectID)
            {
                DeletedSubject(subjectID);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subjectID"></param>
        [Transaction(TransactionMode.Requires)]
        public void DeletedSubject(string subjectID)
        {
            if (!String.IsNullOrEmpty(subjectID))
            {
                Subject item = this.GetSubjectByID(subjectID);
                item.IsDeleted = true;
                subjectRepository.Update(item);
            }
        }

        #endregion

        #region ISubjectsService 成员


        public IList<Subject> GetSubjectsBySubjectCategory(string subjectCategory, bool isDeleted)
        {
            DetachedCriteria criteria = DetachedCriteria.For(typeof(Subject));
            if (!String.IsNullOrEmpty(subjectCategory))
            {
                criteria.Add(Expression.Eq("SubjectCategory", subjectCategory));
            }
            criteria.Add(Expression.Eq("IsDeleted", isDeleted));

            return subjectRepository.FindAll(criteria);
        }

        #endregion
    }
}
